/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.evenmo.string;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * This is Description 测试lamdba
 *
 * @author moyongjun
 * @date 2019/12/07
 */
public class TestLambda {


    public static void main(String[] args) throws ParseException {

        // lambda中的 -> 表示结果是或者返回结果两种情况，无返回时，可用代码块
        //有返回结构
        String in= "test";
        Supplier<String> supplier=() -> in;

        Runnable hello_world = () -> {System.out.println("hello world");};


        Callable<String> stringCallable = () -> in;

        try {
            String call = stringCallable.call();

            System.out.println("call="+call);
        }catch (Exception e){
            e.printStackTrace();
        }

        hello_world.run();

        String s = supplier.get();
        System.out.printf("supplier的结果是：%s", s);





        // 无返回结果
        Consumer<Integer> consumer = (Integer a) -> {
            System.out.println("");
        };

        Consumer<Integer> consumer1=a -> System.out.println("consumer1="+a);
        consumer.accept(1);
        consumer1.accept(2);

        BiConsumer<Integer,String> biConsumer=(a,b) -> System.out.println("bicConsumer="+a+b);

        biConsumer.accept(1, "2");

        // 有返回结果，第二个参数的泛型表示返回类型
        Function<Integer,String> function=a -> a+"";

        String apply = function.apply(1);
        System.out.println("function result="+apply);

        BiFunction<Integer,Integer,String> biFunction=(a,b) -> {return "a+b="+(a+b);};

        String apply1 = biFunction.apply(6, 6);
        System.out.println("apply="+apply1);

//有返回结构，传入类型和返回结果类型是相同的，传入两个参数，返回一个结构
        BinaryOperator<Integer> binaryOperator=(a,b) ->a+b;

        Integer apply2 = binaryOperator.apply(1, 2);
        System.out.println(apply2);

// 断言，返回boolean类型
        Predicate<Integer> predicate= a -> a>=1;

        boolean test = predicate.test(1);
        System.out.println(test);


// 接口只有个个方法
//        Thread thread = new Thread

       Thread t= new Thread(new Runnable() {
           @Override
           public void run() {
               System.out.println("hello");
           }
       });

       t.start();

        Thread t2= new Thread(()->{System.out.println("hello-----t2");});

        t2.start();



        List<String> list = Arrays.asList("a31", "d23","c3", "a14", "sdfdsf");

        list.forEach(System.out::println);

        list.forEach(a->System.out.println(a));

        Consumer<String> pr =a -> System.out.println(a);
        list.forEach(pr);

list.sort(Comparator.comparing(String::toString));

//等价

//        list.sort(
//                (a,b) ->a.compareTo(b)
//        );



        System.out.println(list);



        List<String> collect = list.stream().map(String::toString).collect(Collectors.toList());

        System.out.println(collect);




        Student stu1 = new Student(1, "stu1adsf", 1, Boolean.TRUE);
        Student stu2 = new Student(3, "stu2asdf", 2, Boolean.FALSE);
        Student stu3 = new Student(2, "stu3aa", 1, Boolean.TRUE);
        Student stu4 = new Student(4, "stusd4", 2, Boolean.FALSE);
        Student stu5 = new Student(5, "stusd4", 2, Boolean.FALSE);
        Student stu6 = new Student(6, "stusd4", 2, Boolean.FALSE);
        Student stu7 = new Student(7, "stusd4", 2, Boolean.FALSE);
        List<Student> stus = Arrays.asList(stu1, stu2, stu3, stu4,stu5,stu6,stu7);

//根据性别分组
        Map<Integer, List<Student>> collect1 = stus.stream().collect(Collectors.groupingBy(Student::getSex));


        System.out.println(collect1);

        // 合并姓名 以逗号分割
        String collect2 = stus.stream().map(Student::getName).collect(Collectors.joining(","));

        System.out.println("统计学生人数："+stus.stream().count());

        System.out.println(collect2);


        OptionalInt max = stus.stream().mapToInt(Student::getId).max();
        System.out.println(max);
        int i = max.orElse(0);

        System.out.println(i);


        Optional<Integer> reduce = stus.stream().map(Student::getId).reduce(Integer::sum);


        System.out.println("reduce="+reduce.orElse(0));


        Instant now = Instant.now();
        System.out.println(now);
// 获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("当前时间: " + currentTime);

        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1: " + date1);

        Month month = currentTime.getMonth();
        int day = currentTime.getDayOfMonth();
        int seconds = currentTime.getSecond();

        System.out.println("月: " + month +", 日: " + day +", 秒: " + seconds);

        LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
        System.out.println("date2: " + date2);

        // 12 december 2014
        LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
        System.out.println("date3: " + date3);

        // 22 小时 15 分钟
        LocalTime date4 = LocalTime.of(22, 15);
        System.out.println("date4: " + date4);

        // 解析字符串
        LocalTime date5 = LocalTime.parse("20:15:30");
        System.out.println("date5: " + date5);

        Date date=new Date();

        System.out.println(date);

        System.out.println("-------------------");

        ZonedDateTime date12 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
        System.out.println("date1: " + date12);

        ZoneId id = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId: " + id);

        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("当期时区: " + currentZone);


        List<String> names = Arrays.asList("Jack", "Jill", "Nate", "Kara", "Kim", "Jullie", "Paul", "Peter");

        System.out.println(
                names.stream()
                        .filter(str -> str.length() == 4)
                        .collect(Collectors.joining(", ")));


        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf.parse("2012-12-24 12:23:45");


        String format = sdf.format(new Date());
        System.out.println(format);
        System.out.println(parse);

        String[] strs = {"java8", "is", "easy", "to", "use"};


        String[] split = "sdf,sg".split(",");
        for (String s1 : split) {
            System.out.println(s1);
        }



        List<String> distinctStrs = Arrays.stream(strs)
                .map(str -> str.split(""))
                // 映射成为Stream<String[]>
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());


        System.out.println(distinctStrs);



    }



}
